package ntu.metaweb.storage.local;

import java.io.File;
import java.io.IOException;

import ntu.metaweb.storage.User;

import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;
import com.thoughtworks.xstream.persistence.PersistenceStrategy;
import com.thoughtworks.xstream.persistence.XmlMap;

public class LocalStorageManager {

	private static LocalStorageManager instance = new LocalStorageManager();
	
	public static LocalStorageManager getInstance() {
		System.out.println("test test");
		return LocalStorageManager.instance;
	}
	
	private PersistenceStrategy persistenceStrategy;
	private XmlMap userMap;
	
	private LocalStorageManager() {
		File f = new File("tmp");
		if (!f.exists()) {
			f.mkdir();
		}
		this.persistenceStrategy = new FilePersistenceStrategy(f);
		this.userMap = new XmlMap(persistenceStrategy);
	}
	
	public void addUserData(User user) {
		this.userMap.put(user.getName(), user);
	}
	
	public User getUserData(String username) {
		System.out.println(this.userMap.get(username));
		return (User) this.userMap.get(username);
	}

	public void refreshUserData(User user) {
		System.out.println(user.getTrustedUsers().toString());
		this.userMap.put(user.getName(), user);
	}
	
}
